package com.hairline.service;

import com.hairline.bean.*;
import com.hairline.bean.vo.CategoryL1VO;
import com.hairline.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl implements  GoodsService{


    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    GoodsAttributeMapper goodsAttributeMapper;
    @Autowired
    GoodsSpecificationMapper goodsSpecificationMapper;
    @Autowired
    GoodsProductMapper goodsProductMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    BrandMapper brandMapper;

    @Override
    public List<Goods> selectGoodsbyExample(GoodsExample goodsExamp) {
        List<Goods> goods = goodsMapper.selectByExample(goodsExamp);
        return goods;
    }

    @Override
    public long countByExample(GoodsExample goodsExample) {

        return goodsMapper.countByExample(goodsExample);
    }

    @Override
    public Goods selectById(Integer id) {
        return goodsMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<GoodsAttribute> selectAttributeBygoodsId(Integer goodsId) {
        GoodsAttributeExample goodsAttributeExample = new GoodsAttributeExample();
        GoodsAttributeExample.Criteria criteria = goodsAttributeExample.createCriteria();
        criteria.andGoodsIdEqualTo(goodsId);
        List<GoodsAttribute> attributeList = goodsAttributeMapper.selectByExample(goodsAttributeExample);
        return attributeList;
    }

    @Override
    public List<GoodsSpecification> selectSpecificationByGoodsId(Integer id) {
        GoodsSpecificationExample goodsSpecificationExample = new GoodsSpecificationExample();
        GoodsSpecificationExample.Criteria criteria = goodsSpecificationExample.createCriteria();
        criteria.andGoodsIdEqualTo(id);
        List<GoodsSpecification> goodsSpecifications = goodsSpecificationMapper.selectByExample(goodsSpecificationExample);
        return goodsSpecifications;
    }

    @Override
    public List<GoodsProduct> selectProductByGoodsId(Integer id) {
        GoodsProductExample goodsProductExample = new GoodsProductExample();
        goodsProductExample.createCriteria().andGoodsIdEqualTo(id);
        return goodsProductMapper.selectByExample(goodsProductExample);
    }

    @Override
    public Category selectCategoryIdByGoodsCategoryId(Integer categoryId) {
        return categoryMapper.selectByPrimaryKey(categoryId);
    }

    @Override
    public List<CategoryL1VO> selectCategoryL2(Integer pid) {
        return categoryMapper.selectCategoryL2ByPid(pid);
    }

    @Override
    public String selectcategoryL1Name(Integer pid) {
        return categoryMapper.selectL1NameByid(pid);
    }

    @Override
    public List<CategoryL1VO> selectCategoryL1() {
        return categoryMapper.categoryL1();
    }

    @Override
    public void deleteBygoodsIdOnAttribute(Integer goodSn) {
        goodsAttributeMapper.deleteBygoodsId(goodSn);
    }

    @Override
    public void deleteByIdOnGoods(Integer id) {
        goodsMapper.deleteByPrimaryKey(id);

    }

    @Override
    public void deleteBygoodsIdOnProduct(Integer goodSn) {
        goodsProductMapper.deleteBygoodsId(goodSn);

    }

    @Override
    public void deleteBygoodsIdOnSpecification(Integer goodSn) {
        goodsSpecificationMapper.deleteBygoodsId(goodSn);

    }

    @Override
    public int insertSelectiveOnGoods(Goods goods) {
        goodsMapper.insertSelective(goods);
        int id = goodsMapper.selectLastId();
        return id;
    }

    @Override
    public void insertSelectiveOnAttribute(GoodsAttribute attributes) {
        goodsAttributeMapper.insertSelective(attributes);
    }

    @Override
    public void insertSelectiveOnProducts(GoodsProduct products) {
        goodsProductMapper.insertSelective(products);

    }

    @Override
    public void insertSelectiveOnSpecification(GoodsSpecification goodsSpecification) {
        goodsSpecificationMapper.insertSelective(goodsSpecification);
    }

    @Override
    public List<CategoryL1VO> selectBrands() {
        return brandMapper.selectIdAndName() ;
    }

    @Override
    public int insertSelectiveOnGoodsWithGoodsBO(Goods bo) {
        int id = goodsMapper.selectLastId();
        return id;
    }

    @Override
    public boolean insertSelectiveOnSpecificationAddId(List<GoodsSpecification> specifications, int id) {

        int num = 0;//统计有没有全部数据插入成功
        for (GoodsSpecification specification : specifications) {
            specification.setGoodsId(id);
            int i = goodsSpecificationMapper.insertSelective(specification);
            num += i;
        }
        if (num == specifications.size()){
            return true;
        }
        return false;
    }

    @Override
    public boolean insertSelectiveOnProductsAddId(List<GoodsProduct> products, int id) {
        int num = 0;
        for (GoodsProduct product : products) {
            product.setGoodsId(id);
            int i = goodsProductMapper.insertSelective(product);
            num += i;
        }
        if (num == products.size()){
            return true;
        }
        return false;
    }

    @Override
    public boolean insertSelectiveOnAttributeAddId(List<GoodsAttribute> attributes, int id) {
        int num = 0;
        for (GoodsAttribute attribute : attributes) {
            attribute.setGoodsId(id);
            int i = goodsAttributeMapper.insertSelective(attribute);
            num += i;
        }
        if (num == attributes.size()){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateSelectiveOnGoods(Goods goods) {
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.createCriteria().andIdEqualTo(goods.getId());
        int i = goodsMapper.updateByExampleSelective(goods, goodsExample);
        if (i == 1){
            return true;
        }
        return false;

    }

    @Override
    public boolean updateSelectiveOnSpecification(List<GoodsSpecification> specifications, Integer goodsId) {
        int num = 0;
        for (GoodsSpecification specification : specifications) {
            GoodsSpecificationExample goodsSpecificationExample = new GoodsSpecificationExample();
            goodsSpecificationExample.createCriteria().andGoodsIdEqualTo(goodsId);
            int i = goodsSpecificationMapper.updateByExampleSelective(specification, goodsSpecificationExample);
            num += i;
        }
        if (num == specifications.size()){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateSelectiveOnProduct(List<GoodsProduct> products, Integer goodsId) {
        int num = 0;
        for (GoodsProduct product : products) {
            GoodsProductExample goodsProductExample = new GoodsProductExample();
            goodsProductExample.createCriteria().andGoodsIdEqualTo(goodsId);
            int i = goodsProductMapper.updateByExampleSelective(product, goodsProductExample);
            num += i;
        }
        if (num == products.size()){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateSelectiveOnAttribute(List<GoodsAttribute> attributes, Integer goodsId) {
        int num = 0;
        for (GoodsAttribute attribute : attributes) {
            GoodsAttributeExample goodsAttributeExample = new GoodsAttributeExample();
            goodsAttributeExample.createCriteria().andGoodsIdEqualTo(goodsId);
            int i = goodsAttributeMapper.updateByExampleSelective(attribute, goodsAttributeExample);
            num += i;
        }
        if (num == attributes.size()){
            return true;
        }
        return false;
    }
}
